package com.bocai.grainshop.product.service.impl;


import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bocai.common.utils.PageUtils;
import com.bocai.common.utils.Query;

import com.bocai.grainshop.product.dao.AttrAttrgroupRelationDao;
import com.bocai.grainshop.product.entity.AttrAttrgroupRelationEntity;
import com.bocai.grainshop.product.service.AttrAttrgroupRelationService;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveBatchSort(List<AttrAttrgroupRelationEntity> vos) {
        if(vos.size()>0)
        {
            for (AttrAttrgroupRelationEntity attr: vos
                 ) {
                attr.setAttrSort(0);
            }
            this.saveBatch(vos);
        }
    }

    @Override
    public void removeByMoreIds(List<AttrAttrgroupRelationEntity> vos) {
        QueryWrapper<AttrAttrgroupRelationEntity> queryWrapper = new QueryWrapper<>();
        List<Long> idList = new ArrayList<>();
        if(vos.size()>0){
            for (AttrAttrgroupRelationEntity attr:
                    vos){
                queryWrapper.eq("attr_id",attr.getAttrId()).and((obj)->{
                    obj.eq("attr_group_id",attr.getAttrGroupId());
                });
                AttrAttrgroupRelationEntity one = this.baseMapper.selectOne(queryWrapper);
                idList.add(one.getId());
                queryWrapper = new QueryWrapper<>();
            }
            this.removeByIds(idList);
        }
    }

}